package com.tgpms.web.earlierStage.service.impl;

import java.io.File;
import java.util.Date;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.util.Common;
import com.tgpms.util.FTPUtil;
import com.tgpms.web.earlierStage.dao.DocUploadEntityHistoryMapper;
import com.tgpms.web.earlierStage.dao.DocUploadEntityMapper;
import com.tgpms.web.earlierStage.entity.DocUploadEntity;
import com.tgpms.web.earlierStage.entity.DocUploadEntityHistory;
import com.tgpms.web.earlierStage.service.EalierDocUploadService;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class EalierDocUploadServiceImpl implements EalierDocUploadService {

	@Autowired
	private DocUploadEntityMapper docUploadEntityMapper;
	@Autowired
	private DocUploadEntityHistoryMapper historyMapper;

	//ftp_url
	@Value("${ftpparam.url}")
	private String url;
	@Value("${ftpparam.port}")
	private Integer port;
	@Value("${ftpparam.username}")
	private String username;
	@Value("${ftpparam.password}")
	private String password;
	@Value("${ftpparam.downurl}")
	private String downurl;
	@Value("${ftpparam.filepath3}")
	private String filepath3;
	@Value("${ftpparam.localloadfile}")
	private String localloadfile;

	@Override
	public Result addDocUpload(DocUploadEntity docUpload, Result result) {
		//查询唯一键是否重复
		if(docUploadEntityMapper.selByUniqueKey(docUpload.getTaskId(),docUpload.getSortId())!=null) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT_REPETITION.getExplain());
			return result;
		}
		//主键形式 yyyyMMddHHmmss+UUID
		String id = Common.fromDateFormats()+UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
		docUpload.setId(id);
		docUpload.setRowstamp(docUpload.getId());
		docUpload.setUploadDate(new Date());
		int i = docUploadEntityMapper.insertSelective(docUpload);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
			result.setData(docUpload.getId());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}

	@Override
	public Result updateDocUpload(DocUploadEntity docUpload, Result result) {
		docUpload.setModifyDate(new Date());
		int i = docUploadEntityMapper.updateByPrimaryKeySelective(docUpload);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}

	@Override
	public Result deleteDocUpload(String id, Result result) {
		//查询文件信息
		DocUploadEntity docUpload = docUploadEntityMapper.selectByPrimaryKey(id);
		//删除文件信息
		int i = docUploadEntityMapper.deleteByPrimaryKey(id);
		if(i==1) {
			//删除文件版本
			int j = historyMapper.deleteEntityHistoryByDocId(id);
			if(j>=0) {
				//删除文件
				if(docUpload!=null) {
					String fileDir = localloadfile;
					File dir = new File(fileDir);
					if (!dir.exists()){
						dir.mkdirs();
					}
					File excelFile = new File(dir.getAbsolutePath()+ File.separator+ docUpload.getNewDocName());
					if(excelFile.exists()) {
						excelFile.delete();
					}
					FTPUtil.deleteFile(url, port, username, password, docUpload.getFilePath(), docUpload.getNewDocName());
				}
				result.setMsg(ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
			}else {
				result.setSuccess(false);
				result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
			}
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}

	@Override
	public DocUploadEntity selDocUploadById(String id) {
		DocUploadEntity docUpload = docUploadEntityMapper.selectByPrimaryKey(id);
		return docUpload;
	}

}
